#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QThread>
#include <QMessageBox>
#include <QRandomGenerator>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("文件接收端");
    //主线程负责窗口时间，子线程负责文件传输
    qDebug()<<"当前主线程线程ID:"<<QThread::currentThreadId();
    //设置默认值
    ui->lineEdit_ip->setText("127.0.0.1");
    ui->lineEdit_port->setText("8989");
    ui->progressBar->setValue(0); //设置当前
    //创建子线程
    subThread = new QThread;
    worker = new RecvFile; //不可指定父对象
    //worker想工作，除了subThread start还需要信号量连接调用workin
    worker->moveToThread(subThread); //移动到子线程中区

    subThread->start(); //子线程开始工作

    connect(this,&MainWindow::startConnect,worker,&RecvFile::connectServer);

    //当子线程连接服务器成功，通知主线程
    connect(worker,&RecvFile::connectOK,this,[=]()
            {
                //通知程序的使用者
                QMessageBox::information(this,"提示","已经成功连接到了服务器");
                ui->connect->setText("已连接");
                ui->connect->setEnabled(false);
            });

    //当子线程接收到数据呈现给父进程，将接受的数据呈现到UI上面
    connect(worker,&RecvFile::message,this,[=](QByteArray msg)
            {
                //修改每次发送的不同颜色
                QVector<QColor> colors =
                    {
                        Qt::red,Qt::green,Qt::black,Qt::blue,Qt::darkRed,Qt::cyan,Qt::magenta
                    };
                int index = QRandomGenerator::global()->bounded(colors.size()); //获取colors里面的随机数
                //取出某一种颜色
                ui->textBrowser->setTextColor(colors.at(index)); //设置一种随机颜色

                ui->textBrowser->append(msg);

            });
    //当子进程结束
    connect(worker,&RecvFile::gameover,this,[=]()
            {
                //子线程运行结束，进行资源的释放
                qDebug()<<"子进程文件上传完毕";
            });

    connect(ui->pushButton_open_client,&QPushButton::clicked,this,[=]()
            {
                subThread->quit();
                subThread->wait();
                subThread->deleteLater();
                ui->connect->setEnabled(true);
                ui->connect->setText("连接服务器");
            });

    //当子进程更新总文件大小，设置progressBar
    connect(worker,&RecvFile::total_size_signal,this,[=](qint64 size)
            {
                qDebug()<<"接受文件总大小为"<<size;
                ui->progressBar->setMinimum(0);
                ui->progressBar->setMaximum(static_cast<int>(size));
                ui->progressBar->setValue(0); //设置当前

            });
    connect(worker,&RecvFile::now_size_signal,this,[=](qint64 size)
            {
                //qDebug()<<"接受文件目前大小为"<<size;
                ui->progressBar->setValue(static_cast<int>(size)); //设置当前

            });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_open_client_clicked()
{
    if(ui->pushButton_open_client->text() == "打开客户端")
    {
        //拿到IP端口，将Ip和端口传递给子线程，在子线程new QTcp套接字对象，在子线程中接受数据
        QString ip =ui->lineEdit_ip->text(); //读取ip
        unsigned short port = ui->lineEdit_port->text().toUShort();
        ui->pushButton_open_client->setText("关闭客户端");
        //通过信号传递给子线程
        emit startConnect(ip,port);
    }
    else //关闭客户端
    {
        qDebug() << "关闭客户端";
    }

}

